/* 
 * This file contains the registeration function.
 */

$(document).ready(
    function(){
        var base_url=$('#base-url').text(); // This will be used for all ajax calls as base url
        //Email
        $('#email').live('focusout',function(){
            var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if(!regex.test($(this).val())){
//                $("#email_err").css("display","block");
//                $("#email_err").html('Invalid email').delay(5000).fadeOut(1500);
                $("#error_reg").css("display","block");
                $("#error_reg").html('Invalid email').delay(5000).fadeOut(1500);
            }
            
            $.ajax({
                url: base_url+"/users/check_email",
                type: "POST",
                data: {
                    email : $(this).val()
                    },
                dataType: "html",
                async:false,
                success: function(res){
                    var response=jQuery.trim(res);
                    if(response){
//                        $("#email_err").css("display","block");
//                        $("#email_err").html(x).delay(5000).fadeOut(1500); 
                $("#error_reg").css("display","block");
                $("#error_reg").html(response).delay(5000).fadeOut(1500);
                    }
                }
            });
        });
        
        //rcpassword
        $('#rcpassword').live('focusout',function(){
                if(password_match($('#rpassword').val(),$('#rcpassword').val())){
//                    $("#pass_err").css("display","block");
//                    $("#pass_err").html('').delay(5000).fadeOut(1500);
//                    $("#error_reg").css("display","block");
//                    $("#error_reg").html('').delay(5000).fadeOut(1500);
                }else{
//                    $("#pass_err").css("display","block");
//                    $("#pass_err").html('Password do not match').delay(5000).fadeOut(1500);
                    $("#error_reg").css("display","block");
                    $("#error_reg").html('Password do not match').delay(5000).fadeOut(1500);
                }
        });
            
        //UserRegisterForm
        $("#secureRegister").submit(function (){
            if(!isEmail($('#email').val())){
                $("#error_reg").css("display","block");
                $("#error_reg").html('Invalid email').delay(5000).fadeOut(1500);
                return false;
            }else if(!password_match($('#rpassword').val(),$("#rcpassword").val())){
//                $("#pass_err").css("display","block");
//                $("#pass_err").html('Password do not match').delay(5000).fadeOut(1500);
                $("#error_reg").css("display","block");
                $("#error_reg").html('Passwords do not match').delay(5000).fadeOut(1500);
                return false;
            }
          
          if(isEmail($('#email').val()) && password_match($('#rpassword').val(),$("#rcpassword").val())){
            $.ajax({
                url: base_url+'/users/ajax_register',
                type: "POST",
                data: {
                    email : $('#email').val(),
                    pass:$('#rpassword').val(),
                    captcha:$("#captcha_val").val()
                },
                dataType: "html",
                async:false,
                success: function(res){                    
                    var response=jQuery.trim(res);
                    if(response==''){
                    var data='<div class="register-form" style="padding:30px 0 30px 30px;font-size:16px; font-weight:bold;"><h2>Thank you for Registering with Rate The Universe.</h2>\n\
        <h1>Please Check your email to Activate your account </h1></div>';
                       
//                       $.fancybox(
//                        {'content':data,
//                            'onClosed':function(){                               
//                                //location.reload();
//                            },
//                            'onStart':function(){
//                                //$('#cancelReg').trigger('click');
//                            }
//                        });

                    $("#error_reg").css("display","block");
                    $("#error_reg").html('Thank you for Registering with Rate The Universe. Please Check your email to Activate your account ').delay(5000).fadeOut(1500);
                    $("#secureRegister").delay(5000).fadeOut(1500);
                    $('#refresh-captcha').trigger('click');
                    //$('#cancelReg').trigger('click');

                    }else{
                        $('#refresh-captcha').trigger('click');
                        $("#error_reg").css("display","block");                        
                        $("#error_reg").html(response).delay(5000).fadeOut(1500);
                    }
                }
            });
          }else{
              if(!isEmail($('#email').val())){
//                  $("#email_err").css("display","block");
//                  $("#email_err").html('Field required').delay(5000).fadeOut(1500);
                  $("#error_reg").css("display","block");                        
                  $("#error_reg").html('Invalid email').delay(5000).fadeOut(1500);
              }else if(password_match($('#rpassword').val(),$().val("#rcpassword"))){
//                  $("#pass_err").css('display','block');
//                  $("#pass_err").html('Field required').delay(5000).fadeOut(1500);
                  $("#error_reg").css("display","block");                        
                  $("#error_reg").html('Passwords do not match').delay(5000).fadeOut(1500);
              }else{
//                   $("#email_err").html('');
//                   $("#pass_err").html('');
//                    $("#error_reg").css("display","block");                        
//                    $("#error_reg").html('').delay(5000).fadeOut(1500);
              }              
          }
           return false;
        });
    
    /********* Refresh Captcha**********/
    $('#refresh-captcha').click(function(){
        $.ajax({
            url: base_url+'/users/refresh_captcha',
            success:function(response){
                $('#captch').html(response);
            }
        });
    });
    /********* Refresh Captcha Ends**********/
    
    //secureLogin
    $('#secureLogin').submit(function(){
       if($("#remember_login:checked").length>0){
          var r=1;
       }else{
           r=0;
       }
       $.ajax({
            url: base_url+"/users/check_login",
            type: "POST",
            data: {
                username : $('#username').val(),
                password:$('#password').val(),
                remember:r
                },
            dataType: "html",
            async:false,
            success: function(res){
                var x=jQuery.trim(res);

                var response=$.parseJSON(res);
                if(!response.error){
                   location.reload();
                }else if (response.error){
                    if(response.type=="1"){
                    $("#error").css("display","block");
                    $("#error").html("It seems that You haven't verified your account yet.Please check your email for Verification email and then try again. <span style='font-weight:bold;cursor:pointer; font-size16px;color:#000;' name='"+response.email+"' id='varify_again'>Click Here </span> to resend verification email ");
                }else if(response.type=="2"){
                    $("#error").css("display","block");
                    $("#error").html("Login failed. Invalid username or password");
                }
            }
            }
        });
         
        return false;
    });
    
    //varify_again
    $('#varify_again').live('click',function(){
        
        $.ajax({
                url:base_url+'/users/send_verification_again',
                type:'POST',
                data:{email:$(this).attr('name')},
                success:function(response){
                    var responseObj=$.parseJSON(response);
                    if(!responseObj.error){
                        $.fancybox({
				'overlayShow'	: true,				
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'	  ,
                                'href':'#resend_varification',
                                'onClosed':function(){
                                    location.reload();
                                }
				 
			});
                    }else{
                        $("#error").css("display","block");
                    $("#error").html(responseObj.desc);
                    }
                }
            });
    });
    
    //send_email
    $('#send_email').submit(function (){
        if(isEmail($('#verifi_email').val())){
            $.ajax({
                url:base_url+'/users/send_verification_again',
                type:"POST",
                data:{email:$('#verifi_email').val()},
                sucess:function(response){                    
                }
                
            });
            
        }else{
            $('#invalid_email').css('display','block');
            $('#invalid_email').html('Invalid email').delay(5000).fadeOut(900);            
        }
        
        return false;
    });

        /***************fancybox ends for term and conditions************/
  /********************Refresh captcha *************/
  function isEmail(email){
        var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if(regex.test(email)){
                return true
            } else{
                return false;
            }
    }
    
  function password_match(pass1,pass2){
        if(pass1==pass2 && pass1.length>=1 ){
            return true;   
        }else{
            return false;
        }
    }
});